//
//  HBNavViewController.m
//  导航栏Demo
//
//  Created by 韩冰 on 2017/10/25.
//  Copyright © 2017年 韩冰. All rights reserved.
//

#import "HBNavViewController.h"
#import "HBCommon.h"


@interface HBNavViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIViewController *currentViewController;
@property (nonatomic, assign) BOOL shouldPopItemAfterPopViewController;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, weak) UIView *navLine;
@end

@implementation HBNavViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
//        UIViewController *lastVc = [self.viewControllers lastObject];
        __weak typeof(viewController) weakVc = viewController;
        UIImage *img = [UIImage hb_imagePathWithName:@"return" bundle:@"XFDCommon" targetClass:[self class]];
        UIBarButtonItem *backItem  = [UIBarButtonItem leftItemWithTargeBlock:^{
            [weakVc.navigationController popViewControllerAnimated:YES];
        } image:img highImage:img];
        viewController.navigationItem.leftBarButtonItem = backItem;
        viewController.hidesBottomBarWhenPushed = YES;
    }

    [super pushViewController:viewController animated:animated];
}
- (UIViewController*)popViewControllerAnimated:(BOOL)animated{
    self.shouldPopItemAfterPopViewController = YES;
    return [super popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
    self.shouldPopItemAfterPopViewController = YES;
    return [super popToViewController:viewController animated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated{
    self.shouldPopItemAfterPopViewController = YES;
    return [super popToRootViewControllerAnimated:animated];
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item{
    
    //! 如果应该pop，说明是在 popViewController 之后，应该直接 popItems
    if (self.shouldPopItemAfterPopViewController) {
        self.shouldPopItemAfterPopViewController = NO;
        return YES;
    }
    
    //! 如果不应该 pop，说明是点击了导航栏的返回，这时候则要做出判断区分是不是在 webview 中
    
    [self popViewControllerAnimated:YES];
    return NO;
}

@end
